﻿#region File and License Information
/*
<File>
	<License>
		Copyright © 2009 - 2017, Daniel Vaughan. All rights reserved.
		This file is part of Calcium (http://calciumsdk.net), 
		which is released under the MIT License.
		See file /Documentation/License.txt for details.
	</License>
	<CreationDate>2010-04-22 17:26:58Z</CreationDate>
</File>
*/
#endregion

using System;
using System.Windows;

#if NETFX_CORE
using Windows.UI.Core;
#endif

namespace Outcoder.Concurrency
{
	/// <summary>
	/// The default implementation for an <see cref="IProvider{T}"/> 
	/// providing an <see cref="ISynchronizationContext"/> instance.
	/// </summary>
	public class DualContextProvider : IProvider<ISynchronizationContext>
	{
		public ISynchronizationContext ProvidedItem
		{
			get
			{
#if SILVERLIGHT
				if (Deployment.Current.Dispatcher.CheckAccess())
				{
					return UISynchronizationContext.Instance;
				}
				return ModelSynchronizationContext.Instance;
#elif NETFX_CORE
				if (CoreWindow.GetForCurrentThread().Dispatcher.HasThreadAccess)
				{
					return UISynchronizationContext.Instance;
				}
				throw new NotImplementedException("Not available for WinRT");
#else
				if (Application.Current.Dispatcher.CheckAccess())
				{
					return UISynchronizationContext.Instance;
				}
				return ModelSynchronizationContext.Instance;
#endif
			}
		}
	}
}
